

var validateElements = function(){
	//sets error count to zero, if greater then zero, some data is not valid
	var error_count = 0;

	//var for required element error message.  A custom message can 
	//be set on the input element called error_message
	var require_message = function(id){		
		return $("#" + id).attr('error_message');	
	}

	//these fields are require on form.
	var required_fields = ['FirstName', 'LastName', 'SSN'];
    
    //these fields need to be regex valildated.
    var re_ssn = ['SSN'];
    
    //regex validators
    var re = {SSN : '^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$'};
	
	//these fields are integer fields
	var integer_fields = ['DeskPhone'];
	
	//removes error messages from previous validation.
	$(".ui-state-error").removeClass();
	
	//creates html of errors
	var html_errors = function(){
		var error_link = "";		
		//if fields in the required_fields variable are empty this error is triggered.	
		$.map(required_fields,function(field){
			if( !$("#" + field).val() )	{
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			}
		});		
		
		//if ext field is entererd and malformed, error is triggered
		$.map(integer_fields,function(field){
			if( $("#" + field).val() && isNaN( $("#" + field).val() ) ){
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			}
		});
        
        //regex fields format check, error is triggered if field is malformed
        $.map(re_ssn, function(field){
            if( $("#" + field).val() && !$("#" + field).val().match(new RegExp( re[field] )) ){
                error_count++;
                $("#" + field).addClass('ui-state-error');
                error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
                field + ":</span> " + require_message(field) + "<br />";
            }
        });
		
		return error_link;
	};
	
	//creates jquery ui dialog box html
	$("div.entry").append('<div id="error_dialog" style="width: auto;"><p>' + html_errors() + '</p></div>');
	
	//creates jquery ui dialog box object
	var $error_dialog = $('#error_dialog').dialog({
		autoOpen: false, width: 'auto', modal: true, title: 'The following fields have errors', resizable: false,
		open: function(event, ui) { $("div.ui-dialog-titlebar").width( $(".ui-dialog").width() ); },
		close: function(event, ui) {$(this).dialog( "destroy" ) }
	});

	//if no errors in validation, well... the data is valid!
	if(error_count){
		$error_dialog.dialog('open');
		$("span[id^='error_link_']").click(function(){
			var element_id = "#" + this.id.split("_")[2];
			$.scrollTo(element_id); //jquery pluging ScrollTo
			$error_dialog.dialog('close');
			$(element_id).focus();
		});
		return false;
	}else{return true;}
};


function bindElements(){
	
	//datepicker replacement for cfcalendar.  Flash bad.  Calendar looks bad.
	$("#EstStart_datepicker").datepicker({
		onSelect: function(dateText, inst) { $("#EstStartDate").val(dateText); }									 
	});	
	
	//checkbox toggle to show hidden div's on form
	$("input[id='NonSCFNetworkAccount']").click(function(){
		( $(this).attr('checked') ) ? $("#NA").show() : $("#NA").hide() ;
	});
	
	//checkbox toggle to show hidden div's on form
	$("input[id='NonSCFBadge']").click(function(){
		( $(this).attr('checked') ) ? $("#Badge").show() : $("#Badge").hide() ;
	});
	
	//bind submit button
	$("#Submit").bind('click', function(){
		if(validateElements()){
			$("#frmStep1").submit();
		}
	});
}



$(function(){
	bindElements();
});